﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Nb.Tools.DbManager.Libs.Backups;

namespace Nb.Tools.DbManager
{
    public partial class BackupDbForm : Form
    {
        private readonly OperationFacade _operationFacade;
        
        public BackupDbForm(OperationFacade _operationFacade)
        {
            this._operationFacade = _operationFacade;
            InitializeComponent();
        }

        private void BackupDbForm_Load(object sender, EventArgs e)
        {
            btnQuery_Click(sender,e);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            //DateTime beginDate = new DateTime(2000, 01, 01);
            //DateTime endDate = DateTime.MaxValue;
            //if (this.checkBoxBegin.Checked)
            //{
            //    beginDate = dateTimePickerBegin.Value.Date;
            //}
            //if (this.checkBoxEnd.Checked)
            //{
            //    endDate = dateTimePickerEnd.Value.Date.AddHours(24);
            //}

            //TimeSpan diff = endDate - beginDate;
            //double hours = diff.TotalHours;
            //if (hours < 24)
            //{
            //    MessageBox.Show("起始日其不能大于截止日期");
            //    return;
            //}

            //var listAllBackupFiles = _operationFacade.ListAllBackupFiles()
            //    .Where(x => x.CreatedAt <= endDate && x.CreatedAt >= beginDate)
            //    .ToList();
            //if (listAllBackupFiles.Count > 0)
            //{
            //    this.listViewCount.Items.Clear();
            //    foreach (BackupFile backupFile in listAllBackupFiles)
            //    {
            //        ListViewItem item = new ListViewItem();
            //        item.Text = backupFile.FileName;
            //        item.SubItems.Add(backupFile.CreatedAt.ToString("yyyy年MM月dd日HH:mm:ss"));
            //        item.SubItems.Add(backupFile.FilePath);
            //        item.Tag = backupFile;
            //        this.listViewCount.Items.Add(item);
            //    }
            //}
        }

        private BackupFile currentFile = null;
        private void listViewCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentFile = FindCurrentSelect();
            if (currentFile != null)
            {
                lblMessage.Text = string.Format("当前选择：{0}", currentFile.FileName);
            }
        }

        private BackupFile FindCurrentSelect()
        {
            BackupFile file = null;
            if (this.listViewCount.SelectedItems.Count > 0)
            {
                BackupFile row = this.listViewCount.SelectedItems[0].Tag as BackupFile;
                if (row != null)
                {
                    file = row;
                }
            }

            return file;
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
//            var isCurrentOperationBackuped = _operationFacade.IsCurrentOperationBackuped();
//            if (isCurrentOperationBackuped)
//            {
//                var backupFile = _operationFacade.ListAllBackupFiles().OrderBy(x => x.CreatedAt).First();
//                DialogResult resut = MessageBox.Show(string.Format(@"本次操作已经执行过一次备份，确定要重新执行备份吗？
//重新执行将覆盖原有备份({0})。", backupFile.FileName), @"询问窗口", MessageBoxButtons.OKCancel);
//                if (resut == DialogResult.Cancel)
//                {
//                    MessageBox.Show(@"您取消了操作！");
//                    return;
//                }
//            }

//            var messageResult = _operationFacade.BackupDb();
//            if (messageResult.Success)
//            {
//                _operationFacade.SaveBackupDbResult();
//                btnQuery_Click(sender, e);
//            }
//            MessageBox.Show(messageResult.Message);
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            //if (currentFile == null)
            //{
            //    MessageBox.Show("必须先选择要还原的文件，才能进行操作！");
            //    return;
            //}


            //DialogResult resut = MessageBox.Show("确定要执行数据库还原吗", @"询问窗口", MessageBoxButtons.OKCancel);
            //if (resut == DialogResult.Cancel)
            //{
            //    MessageBox.Show(@"您取消了操作！");
            //    return;
            //}

            //var messageResult = _operationFacade.RestoreDb(currentFile);
            //MessageBox.Show(messageResult.Message);
        }

    }

    public class OperationFacade
    {
    }
}
